<template>
  <div class="article-detail">
    <el-card v-loading="loading" class="detail-card">
      <div v-if="article" class="article-container">
        <div class="header">
          <h1 class="title">{{ article.title }}</h1>
          <div class="meta">
            <el-tag size="small" type="success" effect="dark" v-if="article.category">
              <el-icon><Folder /></el-icon>
              {{ article.category }}
            </el-tag>
            <span class="time">
              <el-icon><Calendar /></el-icon>
              {{ formatDate(article.createTime) }}
            </span>
          </div>
        </div>
        
        <div class="content-wrapper">
          <div class="content markdown-body" v-if="article.content">
            <div v-html="renderedContent"></div>
          </div>
          
          <div class="info-section tags-section" v-if="article.tags">
            <div class="section-title">
              <el-icon><Collection /></el-icon>
              <span>文章标签</span>
            </div>
            <div class="tag-list">
              <el-tag 
                v-for="tag in article.tags.split(',')" 
                :key="tag" 
                size="small" 
                :type="getRandomTagType()"
                effect="light"
                class="tag"
              >
                {{ tag }}
              </el-tag>
            </div>
          </div>

          <div class="info-section qa-section">
            <div class="section-title">
              <el-icon><ChatLineSquare /></el-icon>
              <span>相关问答</span>
              <el-button 
                type="primary" 
                size="small" 
                @click="handleGenerateQA"
                :loading="generatingQA"
                class="generate-btn"
              >
                <el-icon><Plus /></el-icon>
                生成问答
              </el-button>
            </div>
            
            <div v-if="qaList.length > 0" class="qa-list">
              <div v-if="inLoveQAList.length > 0" class="qa-scene">
                <h3 class="scene-title">
                  <el-tag type="danger" effect="dark">热恋中的对话</el-tag>
                </h3>
                <div v-for="qa in inLoveQAList" :key="qa.id" class="qa-item">
                  <div class="question">
                    <el-tag size="small" type="warning">问</el-tag>
                    {{ qa.question }}
                  </div>
                  <div class="answer">
                    <el-tag size="small" type="success">答</el-tag>
                    {{ qa.answer }}
                  </div>
                </div>
              </div>
              
              <div v-if="pursuingQAList.length > 0" class="qa-scene">
                <h3 class="scene-title">
                  <el-tag type="warning" effect="dark">追求中的对话</el-tag>
                </h3>
                <div v-for="qa in pursuingQAList" :key="qa.id" class="qa-item">
                  <div class="question">
                    <el-tag size="small" type="warning">问</el-tag>
                    {{ qa.question }}
                  </div>
                  <div class="answer">
                    <el-tag size="small" type="success">答</el-tag>
                    {{ qa.answer }}
                  </div>
                </div>
              </div>
            </div>

            <el-empty v-else description="暂无问答数据" />
          </div>

          <div class="actions">
            <el-button @click="router.back()" type="primary" plain>
              <el-icon><ArrowLeft /></el-icon>返回
            </el-button>
          </div>
        </div>
      </div>
      <el-empty v-else description="文章不存在" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getArticleDetail } from '@/api/article'
import { generateArticleQA, getArticleQAList } from '@/api/analysis'
import type { Article, ArticleQA } from '@/api/types'
import { formatDate } from '@/utils/format'
import { handleError } from '@/utils/error-handler'
import MarkdownIt from 'markdown-it'
import { 
  Calendar, 
  Collection, 
  ArrowLeft, 
  Folder,
  ChatLineSquare,
  Plus
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const generatingQA = ref(false)
const article = ref<Article | null>(null)
const qaList = ref<ArticleQA[]>([])

const inLoveQAList = computed(() => 
  qaList.value.filter(qa => qa.sceneType === 'IN_LOVE')
)

const pursuingQAList = computed(() => 
  qaList.value.filter(qa => qa.sceneType === 'PURSUING')
)

const md = new MarkdownIt({
  html: true,
  breaks: true,
  linkify: true,
  typographer: true
})

const renderedContent = computed(() => {
  if (!article.value?.content) return ''
  const content = article.value.content.replace(/\n/g, '\n\n')
  return md.render(content)
})

async function fetchArticle() {
  loading.value = true
  try {
    const id = Number(route.params.id)
    if (isNaN(id)) {
      throw new Error('无效的文章ID')
    }
    article.value = await getArticleDetail(id)
    await loadQAList(id)
  } catch (error) {
    handleError(error)
    article.value = null
  } finally {
    loading.value = false
  }
}

async function loadQAList(articleId: number) {
  try {
    qaList.value = await getArticleQAList(articleId)
  } catch (error) {
    handleError(error)
  }
}

async function handleGenerateQA() {
  if (!article.value?.id) return
  
  generatingQA.value = true
  try {
    const newQAList = await generateArticleQA(article.value.id)
    qaList.value = [...newQAList, ...qaList.value]
    ElMessage.success('问答生成完成')
  } catch (error) {
    handleError(error)
  } finally {
    generatingQA.value = false
  }
}

// 随机标签类型
function getRandomTagType() {
  const types = ['', 'success', 'warning', 'info']
  return types[Math.floor(Math.random() * types.length)]
}

onMounted(() => {
  fetchArticle()
})
</script>

<style scoped lang="scss">
.article-detail {
  padding: 24px;
  max-width: 1000px;
  margin: 0 auto;
  min-height: calc(100vh - 60px);
  background-color: #f5f7fa;
  
  .detail-card {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    
    :deep(.el-card__body) {
      padding: 0;
    }
  }
  
  .article-container {
    background: linear-gradient(to bottom, #fff 0%, #f8f9fa 100%);
  }
  
  .header {
    padding: 32px;
    text-align: center;
    border-bottom: 1px solid #ebeef5;
    background: linear-gradient(135deg, #f5f7fa 0%, #fff 100%);
    
    .title {
      margin: 0 0 20px;
      font-size: 32px;
      font-weight: 600;
      color: #303133;
      line-height: 1.4;
    }
    
    .meta {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 16px;
      color: #909399;
      
      .el-tag {
        display: flex;
        align-items: center;
        gap: 4px;
      }
      
      .time {
        font-size: 14px;
        display: flex;
        align-items: center;
        gap: 4px;
        color: #606266;
      }
    }
  }
  
  .content-wrapper {
    padding: 32px;
    
    .content {
      margin-bottom: 40px;
      padding: 32px;
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
    }
  }
  
  .info-section {
    margin-bottom: 32px;
    padding: 24px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    
    &.tags-section {
      background: linear-gradient(135deg, #fff 0%, #f8f9fa 100%);
    }
  }
  
  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
    font-size: 18px;
    font-weight: 600;
    color: #303133;
    
    .el-icon {
      color: #409eff;
    }
    
    .generate-btn {
      margin-left: auto;
      
      .el-icon {
        color: inherit;
      }
    }
  }
  
  .tag-list {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    
    .tag {
      transition: all 0.3s ease;
      
      &:hover {
        transform: translateY(-2px);
      }
    }
  }
  
  .qa-list {
    .qa-scene {
      margin-bottom: 32px;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      .scene-title {
        margin-bottom: 20px;
      }
      
      .qa-item {
        margin-bottom: 16px;
        padding: 20px;
        background-color: #f8f9fa;
        border-radius: 8px;
        transition: all 0.3s ease;
        
        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
        }
        
        .question, .answer {
          display: flex;
          align-items: flex-start;
          gap: 12px;
          line-height: 1.6;
          
          .el-tag {
            margin-top: 3px;
            border-radius: 4px;
          }
        }
        
        .question {
          margin-bottom: 16px;
          font-weight: 500;
        }
        
        .answer {
          color: #606266;
          padding-left: 20px;
          border-left: 2px solid #ebeef5;
        }
      }
    }
  }
  
  .actions {
    margin-top: 32px;
    display: flex;
    justify-content: center;
    
    .el-button {
      padding: 12px 24px;
      font-size: 16px;
    }
  }
}

:deep(.markdown-body) {
  font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif;
  padding: 24px;
  
  h1, h2, h3, h4, h5, h6 {
    margin-top: 32px;
    margin-bottom: 16px;
    font-weight: 600;
    line-height: 1.25;
    
    &:first-child {
      margin-top: 0;
    }
  }
  
  h1 { font-size: 2em; }
  h2 { font-size: 1.5em; }
  h3 { font-size: 1.25em; }
  h4 { font-size: 1em; }
  
  p {
    margin-bottom: 16px;
    line-height: 1.8;
  }
  
  img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 16px auto;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
  
  code {
    padding: 0.2em 0.4em;
    margin: 0;
    font-size: 85%;
    background-color: rgba(27,31,35,0.05);
    border-radius: 3px;
    font-family: Consolas, Monaco, 'Andale Mono', monospace;
  }
  
  pre {
    padding: 16px;
    overflow: auto;
    font-size: 85%;
    line-height: 1.45;
    background-color: #f6f8fa;
    border-radius: 4px;
    margin: 16px 0;
    
    code {
      padding: 0;
      margin: 0;
      background-color: transparent;
      white-space: pre;
      overflow-wrap: normal;
      word-break: normal;
    }
  }
  
  blockquote {
    padding: 0.8em 1em;
    color: #6a737d;
    border-left: 0.25em solid #dfe2e5;
    margin: 16px 0;
    background-color: #f8f9fa;
    border-radius: 0 4px 4px 0;
  }
  
  ul, ol {
    padding-left: 2em;
    margin-bottom: 16px;
    
    li {
      margin-bottom: 8px;
    }
  }
  
  table {
    width: 100%;
    border-collapse: collapse;
    margin: 16px 0;
    
    th, td {
      padding: 8px 16px;
      border: 1px solid #dfe2e5;
      text-align: left;
    }
    
    th {
      background-color: #f6f8fa;
      font-weight: 600;
    }
    
    tr:nth-child(even) {
      background-color: #f8f9fa;
    }
  }
  
  hr {
    height: 1px;
    background-color: #dfe2e5;
    border: none;
    margin: 24px 0;
  }
}
</style> 